using System;
using NopSolutions.NopCommerce.Common.Utils;

namespace NopSolutions.NopCommerce.Web.Modules
{
    public partial class CaptchaControl : BaseNopFrontendUserControl
    {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack)
                RegenerateCode();
        }

        public void RegenerateCode() {
            this.Session["CaptchaImageText"] = CommonHelper.GenerateRandomDigitCode(6);
        }

        public bool ValidateCaptcha() {
            if (String.IsNullOrEmpty(this.txtCode.Text)) {
                this.lblMessageLabel.Text = GetLocaleResourceString("Captcha.Incorrect");
                return false;
            }

            if (this.Session["CaptchaImageText"] == null)
                return false;

            if (this.txtCode.Text == this.Session["CaptchaImageText"].ToString()) {
                return true;
            }
            else {
                this.lblMessageLabel.Text = GetLocaleResourceString("Captcha.Incorrect");
                this.txtCode.Text = "";
                RegenerateCode();
                return false;
            }
        }
    }
}